package com.seasugar.raggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seasugar.raggie.common.R;
import com.seasugar.raggie.model.correlation.SetmealDish;
import com.seasugar.raggie.model.dto.SetmealDto;
import com.seasugar.raggie.model.entity.Category;
import com.seasugar.raggie.model.entity.Dish;
import com.seasugar.raggie.model.entity.Setmeal;
import com.seasugar.raggie.model.vo.SetmealVo;
import com.seasugar.raggie.service.CategoryService;
import com.seasugar.raggie.service.DishService;
import com.seasugar.raggie.service.SetmealDishService;
import com.seasugar.raggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;

    @GetMapping("page")
    public R getAllsetmeal(Integer page,Integer pageSize,String name){
        log.info("页数:{},条数:{}",page,pageSize);
        Page pageInfo=new Page(page,pageSize);
        Page<SetmealVo> setmealVoPageInfo =new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        queryWrapper.like(name!=null,Setmeal::getName,name);
        setmealService.page(pageInfo,queryWrapper);
        //改造Page
        BeanUtils.copyProperties(pageInfo,setmealVoPageInfo,"records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealVo> list=new LinkedList<>();
        for (Setmeal record : records) {
            SetmealVo setmealVo=new SetmealVo();
            BeanUtils.copyProperties(record,setmealVo);
            LambdaQueryWrapper<Category> queryWrapper1=new LambdaQueryWrapper<>();
            queryWrapper1.eq(Category::getId,record.getCategoryId());
            Category category = categoryService.getOne(queryWrapper1);
            setmealVo.setCategoryName(category.getName());
            list.add(setmealVo);
        }
        setmealVoPageInfo.setRecords(list);
        return R.success(setmealVoPageInfo);
    }

    /**
     * 添加套餐分类
     */
    @PostMapping
    public R addsetmeal(@RequestBody SetmealDto setmealDto){
        log.info("添加套餐名称:{}",setmealDto.getName());
        boolean b = setmealService.insertSetmeal(setmealDto);
        if(b)
            return R.success("添加成功");
        else
            return R.error("添加失败");
    }

    /**
     * 修改套餐分类
     */
    @PutMapping
    public R updatesetmeal(@RequestBody SetmealDto setmealDto){
        log.info("修改套餐id:{}",setmealDto.getId());
        boolean b = setmealService.updateSetmealById(setmealDto);
        if(b)
            return R.success("修改成功");
        else
            return R.error("修改失败");
    }

    /**
     * 删除套餐
     */
//    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public R deletesetmeal(String ids){
        log.info("删除套餐id:{}",ids);
        //String转为List
        String[] id = StringUtils.split(ids, ',');
        List<String> list= Arrays.asList(id);
        boolean b = setmealService.deleteByIds(list);
        if(b)
            return R.success("删除成功");
        else
            return R.error("删除失败");
    }


    /**
     * 改变套餐销售状态
     */
    @PostMapping("status/{status}")
    public R updateStatus(@PathVariable("status") Integer status,String ids){
        log.info("改变套餐状态id:{}",ids);
        //String转为List
        String[] id = StringUtils.split(ids, ',');
        List<Setmeal> list=new LinkedList<>();
        for(String i:id)
            list.add(new Setmeal(Long.parseLong(i),status));
        boolean b = setmealService.updateBatchById(list);
        if(b)
            return R.success("修改成功");
        else
            return R.error("修改失败");
    }

    /**
     * 根据套餐id获取套餐
     */
    @GetMapping("{id}")
    public R getSetmealById(@PathVariable("id") Long id) {
        log.info("修改菜品id:{}", id);
        SetmealVo setmealVo = setmealService.getSetmealById(id);
        if (setmealVo != null)
            return R.success(setmealVo);
        else
            return R.error("没有该套餐");
    }

    @Cacheable(value = "setmealCache",key = "'categoryId:'+#categoryId")
    @GetMapping("list")
    public R setmealList(Long categoryId){
        //获取分类下的套餐
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(Setmeal::getCategoryId,categoryId);
        queryWrapper.orderByAsc(Setmeal::getUpdateTime);
        queryWrapper.eq(Setmeal::getStatus,1);
        List<Setmeal> list = setmealService.list(queryWrapper);
        if (list != null)
            return R.success(list);
        else
            return R.error("获取失败");
    }

    @GetMapping("dish/{id}")
    public R getDish(@PathVariable("id") Long id){
        Dish dish = dishService.getById(id);
        Setmeal setmeal = setmealService.getById(id);
        if(dish!=null)
            return R.success(dish);
        else if(setmeal!=null)
            return R.success(setmeal);
        else
            return R.error("找不到该菜品");
    }

}
